package files;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import snazzysprites.SnazzySprites;
import state.ColorPalletState;
import static snazzysprites.SnazzySpritesSettings.*;

/**
 * This class can be used to load color pallet data from an XML 
 * file into a constructed ColorPalletState. Note that the XML
 * file must validate against the color_pallet_settings.xsd
 * schema. This class demonstrates how application settings can
 * be loaded dynamically from a file.
 * 
 * @author  Richard McKenna
 *          Debugging Enterprises
 * @version 1.0
 */
public class ColorPalletLoader 
{    
    /**
     * This method will extract the data found in the provided
     * XML file argument and use it to load the color pallet
     * argument.
     * 
     * @param colorPalletXMLFile Path and file name to an XML
     * file containing information about a custom color pallet. Note
     * this XML file must validate against the aforementioned schema.
     * 
     * @param colorPalletState The state manager for the color
     * pallet, we'll load all the data found in the XML file
     * inside here.
     */
    public void initColorPallet( String colorPalletXMLFile,
                                 ColorPalletState colorPalletState)                             
    {
//        Color[] colorPallet = new Color[20];
//        for (int i = 0; i < 20; i++)
//        {
//            colorPallet[i] = Color.GRAY;
//        }
//        colorPalletState.loadColorPalletState(  colorPallet,
//                                                2,
//                                                12,
//                                                Color.GRAY);
        try
        {
            // THIS WILL DO OUR VALIDATION FOR US
            XMLUtilities xmlUtil = new XMLUtilities();
        
            // LOAD THE XML FILE INTO A DOC.
            Document doc = xmlUtil.loadXMLDocument(colorPalletXMLFile, COLOR_PALLET_SETTINGS_SCHEMA);
            int size = xmlUtil.getIntData(doc, PALLET_SIZE_NODE);
            int rows = xmlUtil.getIntData(doc, PALLET_ROWS_NODE);
            Node defaultColorNode = xmlUtil.getNodeWithName(doc, DEFAULT_COLOR_NODE);
            Color defaultColor = getColor(doc, defaultColorNode, xmlUtil);
            Color[] colorPallet = new Color[size];
            for (int i = 0; i < colorPallet.length; i++)
            {
                colorPallet[i] = Color.GRAY;
            }
            NodeList colorList = doc.getElementsByTagName(PALLET_COLOR_NODE);
            int numFixedColors;
            for (numFixedColors = 0; numFixedColors < colorList.getLength(); numFixedColors++)
            {
                if (numFixedColors >= colorPallet.length)
                {
                    break;
                }
                colorPallet[numFixedColors] = getColor(doc, colorList.item(numFixedColors), xmlUtil);
            }
            colorPalletState.loadColorPalletState(colorPallet, rows, numFixedColors, defaultColor);
            
        }
        catch(InvalidXMLFileFormatException ixffe)
        {
            // IF THIS HAPPENS THERE WERE PROBLEMS WITH THE XML FILE
            // LOADING AND THEREFORE THE PROGRAM SHOULD NOT CONTINUE. 
            // SO WE'LL JUST POST A FEEDBACK MESSAGE TO THE USER
            // AND KILL THE APP
            JOptionPane.showMessageDialog(  SnazzySprites.getSnazzySprites().getGUI(), 
                                            ixffe.getMessage(), 
                                            LOADING_XML_ERROR_TEXT, 
                                            JOptionPane.ERROR_MESSAGE);
            
            // LOG THE ERROR
            Logger.getLogger(SnazzySprites.class.getName()).log(Level.SEVERE, null, ixffe);
            
            // KILL THE APP
            System.exit(0);
        }
    }
    
    private Color getColor(Document doc, Node colorNode, XMLUtilities xmlUtil)
    {
        Node red = xmlUtil.getChildNodeWithName(colorNode, RED_NODE);
        Node green = xmlUtil.getChildNodeWithName(colorNode, GREEN_NODE);
        Node blue = xmlUtil.getChildNodeWithName(colorNode, BLUE_NODE);
                return new Color(Integer.parseInt(red.getTextContent()),
                        Integer.parseInt(green.getTextContent()),
                        Integer.parseInt(blue.getTextContent()),
                        255);
    }
}